package com.example.controller;

import com.example.pojo.Student;
import com.example.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Controller
public class UsersController {
    @Autowired
    private UsersService usersService;

    @RequestMapping("findAll")
    public String findAll(Model model){
        List<Student> list=usersService.findAll();
        model.addAttribute("student",list);
        return "list";
    }


    @RequestMapping("toAdds")
    public String toAdd(Model model){
        Student student = new Student();
        model.addAttribute("students",student);
        return "addStudent";
    }
    @RequestMapping("adds")
    public String add(Student student){
        usersService.add(student);
        return "redirect:/findAll";
    }

    @RequestMapping("findByIds")
    public String findByIds(String id,Model model){
        Student student=usersService.findById(id);
        model.addAttribute("students",student);
        return "updateStudent";
    }


    @RequestMapping("updates")
    public String updates(Student student){
        usersService.updates(student);
        return "redirect:/findAll";
    }

    @RequestMapping("dels")
    public String dels(String id){
        usersService.delete(id);
        return "redirect:findAll";
    }
}
